<template>
	<view>
		<!--基础信息-->
		<view class="btName">基础信息</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">
				<text>工单编号</text>
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="service_no" placeholder="工单编号" />
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">
				<text>工单标题</text>
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="name" placeholder="请输入工单名称" />
		</view>
		<view @click="pickerSelectFun('紧急程度')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">紧急程度</text>
			<view :class="level ? '' : 'cBlack'">
				{{level ? level_name : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<view @click="pickerSelectFun('工单类型')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">工单类型</text>
			<view :class="typename ? '' : 'cBlack'">
				{{typename ? typename : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<!-- 下拉选择确认 -->
		<u-select v-model="selectShow" :list="selectList" @confirm="selectConfirmFun"></u-select>

		<view class="btName">工单内容</view>
		<view class="flex-white-plr26 ptb20 mt32">
			<u-input v-model="content" placeholder="工单内容" maxlength="500" placeholderStyle="color: #303133;"
				type="textarea" />
		</view>
		<!--提交按钮-->
		<view class="submitView">
			<u-button type="primary" class="submitBtn" :ripple="true" ripple-bg-color="#909399" @click="submitBtn">
				{{pageType =='' ? '提交' : '保存'}}
			</u-button>
		</view>
	</view>
</template>

<script>
	let that = '';
	import {
		apiOaService
	} from '@/api/workorder.js'
	export default {
		data() {
			return {
				cardIndex: '',
				service_no: '',
				name: '',
				level: '',
				level_name: '',
				type: '',
				typename: '',
				content: '',
				status: '',
				selectShow: false, //是否显示下拉
				selectList: [], //下拉选择数据
				selectType: '',
				sys_user_id: uni.$userInfo.id, //系统员工
				pageType: '', //当前页操作类型，add/update
				xgInfoObj: {}, //存储修改数据
				isAdd: 0,
				commonData: '' //公共数据 
			}
		},
		onLoad(e) {
			that = this;
			that.cardIndex = e.index || '';
			if (e.type == 'update') {
				that.setDataFun();
				that.pageType = e.type;
				uni.setNavigationBarTitle({
					title: '修改工单'
				})
				console.log(that.pageType);
			}
			//加载公共数据
			that.getCommonData();

		},
		methods: {
			onchange(e) {
				const value = e.detail.value
			},
			onnodeclick(node) {
				console.log('这是点击')
				console.log(node)
				that.owner_dept_id = node.value
			},
			//下拉选择框架
			pickerSelectFun: function(str) {
				if (str == '紧急程度') {
					that.selectList = that.commonData.level;
				} else if (str == '工单类型') {
					that.selectList = that.commonData.type
				}
				that.selectShow = true;
				that.selectType = str;
			},
			selectConfirmFun: function(e) {
				let str = e[0].label;
				let val = e[0].value;
				if (that.selectType == '紧急程度') {
					that.level = val;
					that.level_name=str;
				} else if (that.selectType == '工单类型') {
					that.typename = str;
					that.type = val;
				}
			},
			// 提交按钮
			submitBtn: function() {
				if (that.name == '') {
					uni.showModal({
						title: '提示',
						content: '工单名称不能为空，请输入！',
						showCancel: false
					})
					return
				}
				//请求参数
				let reqObj = {
					req: {
						name: that.name,
						service_no: that.service_no,
						level: that.level,
						level_name:that.level_name,
						type: that.type,
						typename: that.typename,
						content: that.content
					}
				}

				//默认操作
				let action = 'add';
				if (that.pageType == 'update') {
					action = 'update'
					reqObj.req.id = that.xgInfoObj.id;
					uni.showLoading({
						title: '修改中...'
					})

				} else {
					reqObj.req.create_user_id = uni.$userInfo.id;
					uni.showLoading({
						title: '提交中...'
					})
				}
				let reqData = {
					action: action,
					params: reqObj
				}
				console.log(action);
				apiOaService(reqData)
					.then(res => {
						if (action == 'update') {
							uni.showToast({
								title: '修改成功',
								icon: 'none',
								duration: 888,
								mask: true
							})
							//更新列表
							if (that.cardIndex != '') {
								uni.$emit('updateListByIndex', {
									index: that.cardIndex,
									obj: reqObj.req
								});
							}
							setTimeout(() => {
								uni.navigateBack()
							}, 888)

						} else {
							uni.showToast({
								title: '新增成功',
								icon: 'none',
								duration: 1000,
								mask: true
							})

							let addObj = reqObj.req;
							addObj.id = res.data; //增加返回id
							uni.$cardInfo = addObj; //更新值
							if (that.isAdd) {
								uni.$emit('addItemInListFun', addObj)
							}
							setTimeout(() => {
								uni.redirectTo({
									url: './show'
								})
							}, 188)
						}
					})
			},
			// 初始化修改数据
			setDataFun: function() {
				let obj = uni.$cardInfo;
				that.xgInfoObj = uni.$cardInfo;
				that.name = obj.name;
				that.service_no = obj.service_no;
				that.level = obj.level;
				that.level_name = obj.level_arr.name;
				that.typename = obj.typename;
				that.content = obj.content;
			},
			// 获得部门数据
			getCommonData: function() {
				let that = this;
				let reqData = {
					action: 'common',
					params: {

					}
				}
				apiOaService(reqData)
					.then(res => {
						console.log(res.data);
						let dictArr = res.data;
						let newArr = [];
						for (var key in dictArr) {

							console.log(dictArr[key]);

							let rowData = [];
							for (var i = 0; i < dictArr[key].length; i++) {
								rowData.push({
									label: dictArr[key][i].name,
									value: dictArr[key][i].id
								})
							}
							newArr[key] = rowData;
						}
						that.commonData = newArr;
						console.log(that.commonData);
					});
			},
			//载入字典
			getCstDict: function() {
				let that = this;
				that.commonData = uni.getStorageSync('commonData') || '';
				console.log('载入字典');
				console.log(that.commonData);

				if (that.commonData) {
					let reqData = {
						action: 'get_list',
						params: {

						}
					}
					apiCstDict(reqData)
						.then(res => {
							let dictArr = res.data;
							let newArr = [];
							for (var key in dictArr) {
								let rowData = [];
								for (var i = 0; i < dictArr[key].length; i++) {
									rowData.push({
										label: dictArr[key][i].name,
										value: dictArr[key][i].id
									})
								}
								newArr[key] = rowData;
							}
							that.commonData = newArr;
						});
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
		padding-bottom: 260rpx;
	}

	.u-radio {
		width: 200rpx !important;
	}

	.btName {
		margin-bottom: 8rpx;
	}

	.ownerdept {
		border: solid 1px #ffffff !important;
	}

	/deep/ .input-value-border {
		border: solid 1px #ffffff !important;
	}
</style>